package com.yanqu.road.server.pb;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.entity.config.charitable.CharitableSpecialEvent;
import com.yanqu.road.entity.player.UserCharitable;
import com.yanqu.road.entity.player.UserCharitablePosition;
import com.yanqu.road.entity.player.UserCharitableSkill;
import com.yanqu.road.pb.charitable.CharitableProto;
import com.yanqu.road.server.gameplayer.module.charitable.CharitableDoLogicResult;
import com.yanqu.road.server.gameplayer.module.charitable.CharitableDoTenTimeResult;

import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/8/30
 */
public class CharitablePb {

    /**
     * 获取行善返回协议
     *
     * @param ret
     * @param charitableDoLogicResult
     * @param newType
     * @return
     */
    public static CharitableProto.CharitableDoResp.Builder getCharitableDoRespMsg(int ret, CharitableDoLogicResult charitableDoLogicResult, int newType, int todayTimes, int totalTimes) {
        CharitableProto.CharitableDoResp.Builder builder = CharitableProto.CharitableDoResp.newBuilder();
        builder.setRet(ret);
        builder.setBaseResp(getCharitableDoBaseResp(charitableDoLogicResult));
        builder.setNewType(newType);
        builder.setTodayTimes(todayTimes);
        builder.setTotalTimes(totalTimes);
        return builder;
    }

    public static CharitableProto.CharitableDoBaseResp.Builder getCharitableDoBaseResp(CharitableDoLogicResult charitableDoLogicResult) {
        CharitableProto.CharitableDoBaseResp.Builder builder = CharitableProto.CharitableDoBaseResp.newBuilder();
        if (charitableDoLogicResult == null) {
            builder.setBeautyId(0);
            builder.setBoxReward("");
            builder.setEvent("");
            builder.setGlamourAddValue(0);
            builder.setPositionType(0);
        } else {
            builder.setBeautyId(charitableDoLogicResult.getBeautyId());
            builder.setGlamourAddValue(charitableDoLogicResult.getGlamourAddValue());
            builder.setBoxReward(charitableDoLogicResult.getBoxReward());

            CharitableSpecialEvent charitableSpecialEvent = charitableDoLogicResult.getEvent();
            builder.setEvent(charitableSpecialEvent.getGuideId() + "," + charitableSpecialEvent.getRewardType().getValue() + "," + charitableSpecialEvent.getRewardNum());
            builder.setPositionType(charitableDoLogicResult.getPositionType());
        }
        return builder;
    }

    public static CharitableProto.CharitableData.Builder getCharitableDataMsg(UserCharitable userCharitable, Map<Integer, UserCharitablePosition> userCharitableMap, Map<Integer, Map<Integer, UserCharitableSkill>> positionSkillMap) {
        CharitableProto.CharitableData.Builder builder = CharitableProto.CharitableData.newBuilder();
        builder.setTodayTimes(userCharitable.getTodayCharitableTimes());
        builder.setType(userCharitable.getCharitableType());
        builder.setInSystem(userCharitable.isInSystem());

        for (UserCharitablePosition userCharitablePosition : userCharitableMap.values()) {
            CharitableProto.CharitablePosition.Builder charitablePositionBuilder = getCharitablePositionMsg(userCharitablePosition);
            if (positionSkillMap.get(userCharitablePosition.getType()) != null) {
                for (UserCharitableSkill userCharitableSkill : positionSkillMap.get(userCharitablePosition.getType()).values()) {
                    charitablePositionBuilder.addSkill(getCharitableSkillMsg(userCharitableSkill));
                }
            }
            builder.addPosition(charitablePositionBuilder);
        }
        return builder;
    }

    public static CharitableProto.CharitablePosition.Builder getCharitablePositionMsg(UserCharitablePosition userCharitablePosition) {
        CharitableProto.CharitablePosition.Builder builder = CharitableProto.CharitablePosition.newBuilder();
        builder.setType(userCharitablePosition.getType());
        builder.setCharitableTimes(userCharitablePosition.getCharitableForUpgradeTimes());
        return builder;
    }

    public static CharitableProto.CharitableSkill.Builder getCharitableSkillMsg(UserCharitableSkill userCharitableSkill) {
        CharitableProto.CharitableSkill.Builder builder = CharitableProto.CharitableSkill.newBuilder();
        builder.setSkillId(userCharitableSkill.getSkillId());
        builder.setSkillLevel(userCharitableSkill.getLevel());
        return builder;
    }

    public static AbstractMessage.Builder<?> getCharitablePositionUpgradeRespMsg(int ret, int newLevel) {
        CharitableProto.CharitablePositionUpgradeResp.Builder builder = CharitableProto.CharitablePositionUpgradeResp.newBuilder();
        builder.setRet(ret);
        builder.setLevel(newLevel);
        return builder;
    }

    public static CharitableProto.CharitableTenDoResp.Builder getCharitableTenDoResp(CharitableDoTenTimeResult charitableDoTenTimeResult) {
        CharitableProto.CharitableTenDoResp.Builder builder = CharitableProto.CharitableTenDoResp.newBuilder();
        builder.setRet(charitableDoTenTimeResult.getRet());
        builder.setNewType(charitableDoTenTimeResult.getCharitableType());
        builder.setTodayTimes(charitableDoTenTimeResult.getTodayCharitableTimes());

        if (charitableDoTenTimeResult.getAllPositionCharitableTimesMap() != null) {
            for (Map.Entry<Integer, Integer> entry : charitableDoTenTimeResult.getAllPositionCharitableTimesMap().entrySet()) {
                CharitableProto.CharitablePositionDoTimes.Builder timesBuilder = CharitableProto.CharitablePositionDoTimes.newBuilder();
                timesBuilder.setPositionType(entry.getKey());
                timesBuilder.setTimes(entry.getValue());
                builder.addTimes(timesBuilder);
            }
        }

        if (charitableDoTenTimeResult.getCharitableDoLogicResultList() != null) {
            for (CharitableDoLogicResult charitableDoLogicResult : charitableDoTenTimeResult.getCharitableDoLogicResultList()) {
                builder.addBaseResp(getCharitableDoBaseResp(charitableDoLogicResult));
            }
        }
        return builder;
    }
}
